package com.liuyk.commonlib.widget.navigation;

import android.content.Context;
import android.database.DataSetObserver;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.liuyk.commonlib.R;

/**
 * 首页底部导航栏
 * <p>
 * Created by liuyakui on 2020/2/5.
 */
public class BottomNavigationBar extends LinearLayout {
    private LinearLayout mContain;
    private OnBottomTabClickListener mOnBottomTabClickListener;
    private DataSetObserver mDataSetObserver;
    private BaseAdapter mAdapter;

    public BottomNavigationBar(Context context) {
        this(context, null);
    }

    public BottomNavigationBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BottomNavigationBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        final View view = LayoutInflater.from(getContext()).inflate(R.layout.bottom_navigation, this);
        mContain = view.findViewById(R.id.bottom_contain);
    }

    private void setData() {
        if (mAdapter == null || mAdapter.getItems() == null || mAdapter.getItems().isEmpty()) {
            return;
        }

        removeAllViews();
        mAdapter.getViews().clear();

        final int size = mAdapter.getItems().size();
        for (int i = 0; i < size; i++) {
            final int index = i;
            final View itemView = mAdapter.builderItemView(this, index);
            mAdapter.addItemView(itemView);
            LayoutParams params = new LayoutParams(0, LayoutParams.WRAP_CONTENT);
            params.weight = 1;
            itemView.setLayoutParams(params);
            itemView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnBottomTabClickListener != null) {
                        mOnBottomTabClickListener.bottomTabSelect(index);
                    }
                }
            });
            mAdapter.bindItemView(itemView, index);
            mContain.addView(itemView);
        }
    }

    public void setCurrentItem(int index) {
        if (mAdapter != null) {
            mAdapter.clickItem(index);
        }
    }

    public void setAdapter(BaseAdapter adapter) {
        this.mAdapter = adapter;
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (mAdapter != null && mDataSetObserver == null) {
            mDataSetObserver = new MyDataSetObserver();
            mAdapter.registerDataSetObserver(mDataSetObserver);
            mAdapter.notifyDataChange();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mAdapter != null && mDataSetObserver != null) {
            mAdapter.unregisterDataSetObserver(mDataSetObserver);
            mDataSetObserver = null;
        }
    }

    private class MyDataSetObserver extends DataSetObserver {
        @Override
        public void onChanged() {
            super.onChanged();
            setData();
        }

        @Override
        public void onInvalidated() {
            super.onInvalidated();
        }
    }

    public void setOnBottomTabClickListener(OnBottomTabClickListener mOnBottomTabClickListener) {
        this.mOnBottomTabClickListener = mOnBottomTabClickListener;
    }

    public interface OnBottomTabClickListener {
        void bottomTabSelect(int index);
    }
}
